package com.puppet.building182.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.puppet.building182.pojo.Building;
import com.puppet.building182.pojo.MyPageData;
import com.puppet.building182.pojo.Owner;
import com.puppet.building182.pojo.Result;
import com.puppet.building182.pojo.enums.BuildingCode;
import com.puppet.building182.service.OwnerService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/owner")
public class OwnerController {
    @Autowired
    private OwnerService ownerService;

    @GetMapping(value = {"/{pageNum}/{pageSize}","/{pageNum}/{pageSize}/{searchQuery}"})
    public Result<?> getOwners(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize ,   @PathVariable(value = "searchQuery", required = false) String searchQuery) {
       //开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Owner> owners = ownerService.getOwners(searchQuery);
//        .将查询的集合封装到分页对象中
        PageInfo<Owner> info = new PageInfo<>(owners);
        MyPageData<Owner> myPageData = new MyPageData<>(info.getList(), info.getTotal());
        return Result.ok(myPageData);
    }
    @PostMapping
    public Result<?> addOwner(@RequestBody Owner owner) {
        return Result.ok(ownerService.addOwner(owner), BuildingCode.SAVE_SUCCESS.getMessage());
    }
    @DeleteMapping("/{id}")
    public Result<?> deleteOwnerById(@PathVariable("id") Integer id) {
        return Result.ok(ownerService.deleteOwnerById(id),BuildingCode.DELETE_SUCCESS.getMessage());
    }
    @PostMapping("/deleteOwner")
    public Result<?> deleteOwner(@RequestBody List<Integer> owner) {
        return Result.ok(ownerService.deleteOwner(owner),BuildingCode.DELETE_SUCCESS.getMessage());
    }
    @PutMapping
    public Result<?> updateOwner(@RequestBody Owner owner) {
        return Result.ok(ownerService.updateOwner(owner),BuildingCode.SAVE_SUCCESS.getMessage());
    }
    @GetMapping("/getOwnerById/{id}")
    public Result<?> getOwnerById(@PathVariable("id") Integer id) {
        return Result.ok(ownerService.getOwnerById(id));
    }
    @GetMapping("/getOwnerNameAndId")
    public Result<?> getOwnerNameAndId() {
        return Result.ok(ownerService.getOwnerNameAndId());
    }
}
